﻿using Newtonsoft.Json;
using System.Collections.Generic;

namespace DeYi.NuGet.Json
{
    /// <summary>
    /// 
    /// </summary>
    public static class JsonHelper
    {
        /// <summary>
        /// 反序列化Json
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="Json">Json字符串</param>
        /// <returns></returns>
        public static T Deserialize<T>(this string Json) => JsonConvert.DeserializeObject<T>(Json);

        /// <summary>
        /// 序列化Json
        /// </summary>
        /// <param name="Obj">需要序列化的对象</param>
        /// <returns></returns>
        public static string Serialize(this object Obj) => JsonConvert.SerializeObject(Obj);

        /// <summary>
        /// 反序列化为泛型列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static List<T> DeserializeToList<T>(this string json)
        {
            if (json != null)
            {
                return JsonConvert.DeserializeObject<List<T>>(json);
            }
            return null;
        }
    }
}
